import Vue from 'vue'
// 我们之前引入vue-router  VueRouter
import Router from 'vue-router'

// use使用插件 注册
Vue.use(Router)

/* Layout */
// 导入布局组件
// 直接import的组件，在我们import该文件的时候 就会加载
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */

// 懒加载说明 语法是：component: () => import('...') 箭头函数
// 路径访问后 才会执行箭头函数 返回值 - 导入的组件 懒加载
// 懒加载目的 - 提高性能 白屏现象 加载过慢
// 懒加载什么时候用 - 一般就是这个页面 用户需要操作 点击菜单才进入，不是一开始就能访问的 配置懒加载
export const constantRoutes = [
  {
    path: '/login',
    // component 箭头函数 - 懒加载
    component: () => import('@/views/login/index'),
    // hidden属性会在之后课上讲到左侧菜单的时候再说 hidden-初步猜测 隐藏
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    // hidden: true,
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      // 之后也会分析 元数据 配置个对象，给当前组件设置一些额外的信息
      meta: { title: 'Dashboard', icon: 'dashboard' }
    }]
  },

  {
    path: '/example',
    component: Layout,
    redirect: '/example/table',
    name: 'Example',
    meta: { title: 'Example', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'table',
        name: 'Table',
        component: () => import('@/views/table/index'),
        meta: { title: 'Table', icon: 'table' }
      },
      {
        path: 'tree',
        name: 'Tree',
        component: () => import('@/views/tree/index'),
        meta: { title: 'Tree', icon: 'tree' }
      }
    ]
  },

  {
    path: '/form',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'Form',
        component: () => import('@/views/form/index'),
        meta: { title: 'Form', icon: 'form' }
      }
    ]
  },

  {
    path: '/nested',
    component: Layout,
    redirect: '/nested/menu1',
    name: 'Nested',
    meta: {
      title: 'Nested',
      icon: 'nested'
    },
    children: [
      {
        path: 'menu1',
        component: () => import('@/views/nested/menu1/index'), // Parent router-view
        name: 'Menu1',
        meta: { title: 'Menu1' },
        children: [
          {
            path: 'menu1-1',
            component: () => import('@/views/nested/menu1/menu1-1'),
            name: 'Menu1-1',
            meta: { title: 'Menu1-1' }
          },
          {
            path: 'menu1-2',
            component: () => import('@/views/nested/menu1/menu1-2'),
            name: 'Menu1-2',
            meta: { title: 'Menu1-2' },
            children: [
              {
                path: 'menu1-2-1',
                component: () => import('@/views/nested/menu1/menu1-2/menu1-2-1'),
                name: 'Menu1-2-1',
                meta: { title: 'Menu1-2-1' }
              },
              {
                path: 'menu1-2-2',
                component: () => import('@/views/nested/menu1/menu1-2/menu1-2-2'),
                name: 'Menu1-2-2',
                meta: { title: 'Menu1-2-2' }
              }
            ]
          },
          {
            path: 'menu1-3',
            component: () => import('@/views/nested/menu1/menu1-3'),
            name: 'Menu1-3',
            meta: { title: 'Menu1-3' }
          }
        ]
      },
      {
        path: 'menu2',
        component: () => import('@/views/nested/menu2/index'),
        name: 'Menu2',
        meta: { title: 'menu2' }
      }
    ]
  },

  {
    path: 'external-link',
    component: Layout,
    children: [
      {
        path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
        meta: { title: 'External Link', icon: 'link' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

// 创建VueRouter实例 为什么不是new VueRouter 因为导入的时候改了名字
// createRouter是个箭头函数 返回值vue router实例
// 为什么这里会包装一个函数 - 函数是干嘛的？封装 复用 抽离逻辑
const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
  /**
   * 以前的写法
   * routes: [
   *  {path: '', compnent: }
   * ]
   * 现在的写法
   * 只是把我们的数组。单独提出去个变量
   */
})

// xxx() - 说明createRouter是个函数
// 这里执行后返回的就是vueRouter实例
const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// 重置路由 - 这里的实现 是花裤衩大佬提供的方案，是固定写法 - 重新实例化 + matcher覆盖
// 重置路由必须要重新实例化我们的vueRouter并且覆盖matcher属性
// 最好讲到权限这一块会用到这个方法，为什么要用？
// A员工 - 对应的角色 - 对应的权限 -> a,b,c,d,e菜单访问权
// B员工 - 对应的角色 - 对应的权限 -> a,b菜单访问权
// 发现不重置路由，他还是能访问a,b,c,d,e -> 解决方案 - 重置路由
export function resetRouter() {
  // 重新实例化VueRouter
  const newRouter = createRouter()
  // 实例化router以后，需要把matcher属性覆盖
  router.matcher = newRouter.matcher // reset router
}

// 默认导出
export default router
